package tuples;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * The StringPair class. Used to simulate a Python tuple, except
 * storing only two elements. Main use for storing the key, value of a Hashtable.
 * 
 * @author Shimu
 *
 */
public class StringPair {

    private String key;

    private String value;

    public StringPair(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    /**
     * Sort the given List<StringPair> alphabetically by keys using 
     * Java's String.CASE_INSENSITIVE_ORDER. 
     * This method is not case sensitive. 
     * 
     * @param array the array to be sorted.
     */
    public static void sortByKey(List<StringPair> array) {
        /**
         * Compare by keys		
         */
        final class CompareByKey implements Comparator<StringPair> {

            public CompareByKey() {
            }

            @Override
            public int compare(StringPair pair1, StringPair pair2) {
                return String.CASE_INSENSITIVE_ORDER.compare(pair1.key,
                        pair2.key);
            }
        }

        Collections.sort(array, new CompareByKey());
    }

    /**
     * Sort the given List<StringPair> alphabetically by value using 
     * Java's String.CASE_INSENSITIVE_ORDER.
     * This method is not case sensitive. 
     * 
     * @param array the array to be sorted.
     */
    public static void sortByValue(List<StringPair> array) {

        /**
         * Compare by values		
         */
        final class CompareByValue implements Comparator<StringPair> {

            public CompareByValue() {
            }

            @Override
            public int compare(StringPair pair1, StringPair pair2) {
                return String.CASE_INSENSITIVE_ORDER.compare(pair1.value,
                        pair2.value);
            }
        }

        Collections.sort(array, new CompareByValue());
    }

    /**
     * Return the String representation of this StringPair. The returned String
     * is of the form:
     * "(key, value)"
     */
    @Override
    public String toString() {
        return ("(" + key + ", " + value + ")");
    }
}
